package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.BasisManageService;

import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.basisManage.BasisSaveRequest;
import com.ktwlsoft.archivemanage.payload.basisManage.BasisUpdateRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;

/**
 * @author JokerMo
 * @version 1.0
 * @date 2020/4/28 15:41
 */
@Api(value = "依据管理", description = "依据管理")
@RestController
@RequestMapping("basisManage")
public class BasisManageController {

    public final BasisManageService basisManageService;

    public BasisManageController(BasisManageService basisManageService) {
        this.basisManageService = basisManageService;
    }

    /**
     * 依据分页查询
     *
     * @param pn
     * @param pageSize
     * @param basisName
     * @return
     */
    @ApiOperation(value = "依据分页查询", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "/findPage")
    @UserAuthenticate(actionCode = "BASISMANAGE_PAGE")
    public ResponseEntity<?> findPage(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize,
                                      @RequestParam(defaultValue = "") String basisName) throws UnsupportedEncodingException {
        return ResponseEntity.ok(basisManageService.findPage(pn, pageSize, basisName));
    }

    /**
     * 获取所有依据
     *
     * @return
     */
    @ApiOperation(value = "获取所有依据", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "/getAll")
    public ResponseEntity<?> getAll() throws UnsupportedEncodingException {
        return ResponseEntity.ok(basisManageService.getAll());
    }

    /**
     * 依据新增
     *
     * @param request
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "依据新增", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "/save")
    @UserAuthenticate(actionCode = "BASISMANAGE_ADD")
    public ResponseEntity<?> save(@Valid @RequestBody BasisSaveRequest request) throws UnsupportedEncodingException {
        return ResponseEntity.ok(basisManageService.save(request));
    }

    /**
     * 依据更新
     *
     * @param request
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "依据更新", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "/update")
    @UserAuthenticate(actionCode = "BASISMANAGE_EDIT")
    public ResponseEntity<?> update(@Valid @RequestBody BasisUpdateRequest request) throws UnsupportedEncodingException {
        return ResponseEntity.ok(basisManageService.update(request));
    }

    /**
     * 依据删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "依据删除", httpMethod = "DELETE", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @DeleteMapping(value = "/deleteById")
    @UserAuthenticate(actionCode = "BASISMANAGE_DELETE")
    public ResponseEntity<?> deleteById(@ApiParam(name = "id", value = "依据id") @RequestParam(defaultValue = "", required = true) Long id) {
        return ResponseEntity.ok(basisManageService.deleteById(id));
    }

    /**
     * id获取依据详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "id获取依据详情", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "/getById")
    public ResponseEntity<?> getById(@ApiParam(name = "id", value = "依据id") @RequestParam(defaultValue = "", required = true) Long id) throws UnsupportedEncodingException {
        return ResponseEntity.ok(basisManageService.getById(id));
    }
}
